package com.woniuxy.ticketmovie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.ticketmovie.entity.ResponseResult;
import com.woniuxy.ticketmovie.entity.TicketMovie;
import com.woniuxy.ticketmovie.entity.dto.MovieDTO;
import com.woniuxy.ticketmovie.entity.dto.MovieQO;
import com.woniuxy.ticketmovie.service.MovieService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName MovieController
 * @Description TODO
 * @Author zx
 * @Date 2021-02-22 19:19:29
 * @Version 1.0
 */
@Slf4j
@Controller
@RequestMapping("movie")
public class MovieController {

    @Resource
    private MovieService movieService;

    @ResponseBody
    @RequestMapping(value = "findById", method = RequestMethod.GET)
    public ResponseResult<TicketMovie> findById(Integer id) {
        ResponseResult<TicketMovie> responseResult = new ResponseResult<>();
        if (movieService.getById(id) == null) {
            responseResult.setCode(500);
            responseResult.setMsg("暂无信息！");
            return responseResult;
        } else {
            responseResult.setCode(200);
            responseResult.setMsg("查询成功！");
            responseResult.setData(movieService.getById(id));
            return responseResult;
        }
    }

    @ResponseBody
    @PostMapping("add")
    public ResponseResult<Void> add(@RequestBody MovieDTO movieDTO) {
        log.info("电影名：{}，电影导演;{}", movieDTO.getMovie().getName(), movieDTO.getMovie().getDirector());
        for (int typeId : movieDTO.getTypeIds()) {
            log.info("类型ID:{}", typeId);
        }
        movieService.add(movieDTO.getMovie(), movieDTO.getTypeIds());
        return ResponseResult.SUCCESS;
    }

    @ResponseBody
    @GetMapping("list")
    public ResponseResult<Page<TicketMovie>> list(@RequestBody MovieQO movieQO) {

        QueryWrapper<TicketMovie> quQueryWrapper = new QueryWrapper<>();
//        StringUtils.isEmpty();
        if (movieQO.getName() != null && movieQO.getName().trim().length() != 0) {
            quQueryWrapper.like("name", movieQO.getName());
        }
        if (movieQO.getDirector() != null && movieQO.getDirector().trim().length() != 0) {
            quQueryWrapper.eq("director", movieQO.getDirector());
        }
        Page<TicketMovie> page = new Page<>(movieQO.getP() == null ? 1 : movieQO.getP(), 2);

        return new ResponseResult<>(movieService.page(page, quQueryWrapper));
    }
}
